package cz.muni.fi.pa165.service.local.impl;

import cz.muni.fi.pa165.dao.CustomerDAO;
import cz.muni.fi.pa165.dto.CustomerDTO;
import cz.muni.fi.pa165.entity.Customer;
import cz.muni.fi.pa165.service.local.CustomerServiceLocal;
import cz.muni.fi.pa165.shiro.ShiroSecured;
import cz.muni.fi.pa165.transfers.CustomerT;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Named;
import org.apache.shiro.authz.annotation.RequiresRoles;
/**
 *
 * @author Jakub Janco
 */
@ApplicationScoped
@Named
@ShiroSecured
@RequiresRoles("Manager")
public class CustomerService implements CustomerServiceLocal {

    @EJB 
    private CustomerDAO customerDAO;
    
    @Override
    public Long createCustomer(CustomerDTO customer) {
        Customer entity = CustomerT.dTOtoEntity(customer);
        customerDAO.createCustomer(entity);
        return entity.getId();
    }

    @Override
    public void removeCustomer(CustomerDTO customer) {
        customerDAO.removeCustomer(CustomerT.dTOtoEntity(customer));
    }

    @Override
    public void updateCustomer(CustomerDTO customer) {
        customerDAO.updateCustomer(CustomerT.dTOtoEntity(customer));
    }

    @Override
    @Produces
    @Named("customers")
    public List<CustomerDTO> getAllCustomers() {
        List<Customer> customers = customerDAO.getAllCustomers();
        List<CustomerDTO> customersDTO = new ArrayList<CustomerDTO>();
        for(Customer pkg : customers) {
            customersDTO.add(CustomerT.entityToDTO(pkg));
        }
        return customersDTO;
    }

    @Override
    public CustomerDTO getCustomerById(Long id) {
        return CustomerT.entityToDTO(customerDAO.getCustomerById(id));
    }
    
}
